﻿@page "/login/{role}"

<PageTitle>研究生住宿信息核对系统</PageTitle>

@inject NavigationManager Navigation
@inject StudentClient StudentClient
@inject TeacherClient TeacherClient
@inject SchoolClient SchoolClient

<div style="max-width: 450px; top: calc(50% - 182.5px); position: absolute; right: 0px; left: 0px; margin: auto;">
  <div style="box-shadow: rgba(0, 0, 0, 0.1) 0px 0.5rem 2rem; background-color: transparent; border-radius: 0.25rem">
    <div class="row">
      <div class="col-md-12 order-md-1 bg-light">
        <div class="px-lg-4 py-md-4 py-lg-4">
          <div class="mb-2 text-center">
            <p class="font-size-h1 font-w700">
              <span class="text-dark">研究生住宿信息核对系统</span>
            </p>
            <p class="text-uppercase font-w700 font-size-sm text-muted">Whut UniTeam ©2022-now</p>
          </div>
          <div class="form-group">
            <Input Size="@InputSize.Large" @bind-Value="@userName" placeholder="用户名"/>
          </div>
          <div class="form-group">
            <InputPassword Size="@InputSize.Large" @bind-Value="@passWord" placeholder="密码" OnPressEnter="@Submit"/>
          </div>
          <div class="form-group">
            <Button OnClick="Submit" Type="@ButtonType.Primary" class="btn-block">
              <span>登录</span>
            </Button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

@if (validationError)
{
    <Alert Type="@AlertType.Error"
       Message="错误"
       Description="@errorMessage"
       ShowIcon="true" 
       Closable 
       AfterClose="ErrorClosed"/>
}

@code {
    [Parameter]
    public string? Role { get; set; }

    string userName { get; set; } = string.Empty;
    string passWord { get; set; } = string.Empty;

    bool validationError = false;
    string errorMessage = string.Empty;

    protected override void OnParametersSet()
    {
        if (Role is not ("student" or "teacher" or "school")) Navigation.NavigateTo("/notfound");
    }

    private async Task Submit()
    {
        validationError = false;
        if (string.IsNullOrWhiteSpace(passWord)) SetError("密码不能为空");
        if (string.IsNullOrWhiteSpace(userName)) SetError("用户名不能为空");

        switch (Role)
        {
            case "student": await StudentLogin(); break;
            case "teacher": await TeacherLogin(); break;
            case "school" : await SchoolLogin(); break;
        }
    }

    private void ErrorClosed()
    {
        validationError = false;
    }

    private void SetError(string msg)
    {
        validationError = true;
        errorMessage = msg;
    }

    private async Task StudentLogin()
    {
        if (validationError) return;

        var success = await StudentClient.Login(userName, passWord);
        if (!success) SetError("登陆失败，用户名或密码错误！");
        else Navigation.NavigateTo("/StudentProfile");
    }

    private async Task TeacherLogin()
    {
        if (validationError) return;
        var success = await TeacherClient.Login(userName, passWord);
        if (!success) SetError("登陆失败，用户名或密码错误！");
        else Navigation.NavigateTo("/RecordReview");
    }

    private async Task SchoolLogin()
    {
        if (validationError) return;
        var success = await SchoolClient.Login(userName, passWord);
        if (!success) SetError("登陆失败，用户名或密码错误！");
        else Navigation.NavigateTo("/SchoolOutput");
    }
}

<style>
.font-w700 {
    font-weight: 700!important;
}

.font-size-h1 {
    font-size: 2.25rem;
}

.font-size-h2 {
    font-size: 1.5rem;
}

.font-size-sm {
    font-size: 0.75rem;
}

.form-group {
    margin-bottom: 1.25rem;
}

.btn-block {
    display: block;
    width: 100%;
}
</style>
